﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.LabEvaluation
{
    public partial class AddLabEvaluation : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYSZP");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                BindData();
            }
        }

        //绑定
        protected void BindData()
        {
            LabDropDownList();//评估对象

            //评估标准类别
            KindDDL.DataSource = DictionaryDropdownlist("119");
            KindDDL.DataBind();

            //主题名
            if (KindDDL.SelectedValue != "")
            {
                int code;
                if (int.TryParse(KindDDL.SelectedValue, out code))
                {
                    TitleDropDownList(code);
                }
            }

            //gridview
            if (TitleDDL.SelectedValue != "")
            {
                int titleID;
                if (int.TryParse(TitleDDL.SelectedValue, out titleID))
                {
                    LabEvalGV_DataBind(titleID);
                }
            }
        }

        //DropDownList索引改变  标准类型
        protected void KindDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            int code = int.Parse(KindDDL.SelectedValue);
            TitleDropDownList(code);//绑定主题
            //绑定Gridview
            int ID = 0;
            if (TitleDDL.SelectedValue != "")
            {
                int.TryParse(TitleDDL.SelectedValue, out ID);
            }
            LabEvalGV_DataBind(ID);
        }

        //DropDownList索引改变  主题
        protected void TitleDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            int ID = 0;
            if (TitleDDL.SelectedValue != "")
            {
                int.TryParse(TitleDDL.SelectedValue, out ID);
            }
            LabEvalGV_DataBind(ID);
        }

        //绑定GridView
        protected void LabEvalGV_DataBind(int TitleID)
        {
            LabMS.BLL.Standard sta = new LabMS.BLL.Standard();
            List<LabMS.Model.Standard> staModelList = new List<LabMS.Model.Standard>();

            staModelList = sta.GetModelList("EvTitleID=" + TitleID);
            LabEvalGridView.DataSource = HandleList(staModelList);
            LabEvalGridView.DataBind();
        }

        //处理List
        protected List<LabMS.Model.Standard> HandleList(List<LabMS.Model.Standard> list)
        {
            List<LabMS.Model.Standard> modelList = new List<LabMS.Model.Standard>();

            for (int i = 0; i < list.Count; i++)
            {
                LabMS.Model.Standard sta = new LabMS.Model.Standard();
                sta.EvTitleID = list[i].EvTitleID;
                sta.ID = list[i].ID;
                sta.St_Content = list[i].St_Content;
                sta.St_Explain = list[i].St_Explain.Replace("\r\n", "<br/>");
                sta.St_Method = list[i].St_Method.Replace("\r\n", "<br/>");
                sta.St_Num = list[i].St_Num;
                sta.St_Standard = list[i].St_Standard.Replace("\r\n", "<br/>");

                modelList.Add(sta);
            }
            return modelList;
        }

        //绑定评估实验室
        protected void LabDropDownList()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> labModel = new List<LabMS.Model.Lab>();

            labModel = lab.GetModelList("Lab_ParentID is null order by [ID] DESC");
            LabDDL.DataSource = labModel;
            LabDDL.DataBind();
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass='" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定主题
        protected void TitleDropDownList(int code)
        {
            LabMS.BLL.EvaluationTitle evalTitle = new LabMS.BLL.EvaluationTitle();
            List<LabMS.Model.EvaluationTitle> evalTitleModel = new List<LabMS.Model.EvaluationTitle>();

            evalTitleModel = evalTitle.GetModelList("Title_Category=" + code);
            TitleDDL.DataSource = evalTitleModel;
            TitleDDL.DataBind();
        }

        //获取评估次数
        protected int GetMaxEvTime(int TitleID, int LabID)
        {
            ExtendBLL.Standard sta = new ExtendBLL.Standard();
            DataSet ds = new DataSet();
            int Time;

            ds = sta.GetMaxTime(TitleID, LabID);
            if (ds.Tables[0].Rows.Count == 0 || ds.Tables[0].Rows[0][0].ToString()=="")
            {
                Time = 1;
            }
            else
            {
                Time = int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
            } 
            return Time;
        }

        //分页
        protected void LabEvalGV_OnPageChange(object sender, GridViewPageEventArgs e)
        {
            LabEvalGridView.PageIndex = e.NewPageIndex;
            LabEvalGV_DataBind(int.Parse(TitleDDL.SelectedValue));
        }

        //保存  
        protected void Save_OnClick(object sender, EventArgs e)
        {
            List<string[]> list = new List<string[]>();
            
            int count = LabEvalGridView.Rows.Count;
            int EvTime;
            for (int i = 0; i < count; i++)
            {
                string[] str = new string[4];
                TextBox selftb = (TextBox)LabEvalGridView.Rows[i].FindControl("SelfEvalTBX");
                TextBox resulttb = (TextBox)LabEvalGridView.Rows[i].FindControl("EventTBX");
                RadioButton rbl = (RadioButton)LabEvalGridView.Rows[i].FindControl("IsRBL1");

                str[0] = selftb.Text.Trim();
                str[1] = resulttb.Text.Trim();
                if (rbl.Checked == true)
                {
                    str[2] = "1";
                }
                else
                {
                    str[2] = "0";
                }
                str[3] = LabEvalGridView.DataKeys[i]["ID"].ToString();
                list.Add(str);
            }
            EvTime = GetMaxEvTime(int.Parse(TitleDDL.SelectedValue), int.Parse(LabDDL.SelectedValue));
            ExtendBLL.Standard stan = new ExtendBLL.Standard();
            try
            {
                stan.AddSelfInfo(int.Parse(LabDDL.SelectedValue), list,EvTime);
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('添加成功！');refreshParent();</script>");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
